package com.fuluwa.interfaces.security.facade;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.fuluwa.domain.bank.model.Account;
import com.fuluwa.domain.bank.model.Account.AccountType;
import com.fuluwa.domain.bank.service.AccountService;
import com.fuluwa.domain.security.model.User;
import com.fuluwa.domain.security.repository.UserRepository;
import com.fuluwa.domain.usercenter.model.Contact;
import com.fuluwa.domain.usercenter.model.UserBasicInfo;
import com.fuluwa.domain.usercenter.model.UserClass;
import com.fuluwa.domain.usercenter.model.UserProfile;
import com.fuluwa.domain.usercenter.respository.UserProfileRepository;
import com.fuluwa.infra.exception.PersistenceException;
import com.fuluwa.infra.jms.MessageProducer;

@Component
public class UserServiceFacadeImpl implements UserServiceFacade {
	@Autowired
	UserRepository userRepository;
	@Autowired
	MessageProducer messageProducer;
	@Autowired
	UserProfileRepository userProfileRepository;
	
	@Autowired
	AccountService accountService;
	
	@Override
	@Transactional
	public void registerUser(UserDTO userDTO) throws PersistenceException {
		User user = userDTO.toUser();
//		user.getUserAttribute().save();
		user.save();
		
		UserClass c=new UserClass(1L);
//		UserClass c=new UserClass("Normal");  
//		c.save();
		
		UserProfile profile=new UserProfile(user, c);
		profile.save();
		
		Account cash=accountService.openingAccount(user, AccountType.CASH);
		Account gold=accountService.openingAccount(user, AccountType.GOLD);
		
		messageProducer.sendQueue(user);
	}

	@Override
	public boolean checkUserByLoginName(String loginName) {
		User user = userRepository.findByLoginName(loginName);
		return (user!=null);
	}

	@Override
	@Transactional
	public void changeUserPassword(String loginName, String oldPassword, String newPassword) throws PersistenceException {
		User user = userRepository.findByLoginName(loginName);
		user.updatePassword(newPassword);
		user.update();
	}

	public void setMessageProducer(MessageProducer messageProducer) {
		this.messageProducer = messageProducer;
	}

	@Override
	public String getUserNo(String loginName) {
		// TODO Auto-generated method stub
		User user = userRepository.findByLoginName(loginName);
		return userProfileRepository.findByUser(user).getUserNo();
	}

	@Override
	@Transactional
	public void updateContact(String userNo, Contact contact) throws PersistenceException {
		// TODO Auto-generated method stub
		UserProfile userprofile = userProfileRepository.findOne(userNo);
		userprofile.updateContact(contact);
	}

	@Override
	@Transactional
	public void updateUserBasicInfo(String userNo, UserBasicInfo userBasicInfo) throws PersistenceException {
		// TODO Auto-generated method stub
		UserProfile userprofile = userProfileRepository.findOne(userNo);
		userprofile.updateUserBasicInfo(userBasicInfo);
	}

}
